﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using System.Xml.Serialization;

namespace Gamev1
{
    public class Platform : Sprite
    {
        #region TypePlatform enum
        public enum TypePlatform
        {
            Impassable,
            Passable,
            Movable
        }
        #endregion

        #region Fields
        public Rectangle top, bottom, left, right;
        private SpriteFont font;
        public TypePlatform platformType;
        public bool isTouchingTop;
        private float elapsedTime;
        public Vector2 startPosition;
        public Vector2 Velocity;
        public bool movingUp = true;
        public bool movingLeft = true;
        private bool reachedEndPoint;
        public bool movesVertical;
        public string naam;
        public float delay = 0f;
        #endregion

        public Platform()
        {
            isTouchingTop = false;
        }

        public void initializeRectangles()
        {
            top = new Rectangle((int)Position.X + 5, (int)Position.Y - 10, Rectangle.Width - 10, 10);
            left = new Rectangle((int)Position.X - 10, (int)Position.Y + 5, 10, Rectangle.Height - 10);
            right = new Rectangle((int)Position.X + Rectangle.Width, (int)Position.Y + 5, 10, Rectangle.Height - 10);
            bottom = new Rectangle((int)Position.X + 5, (int)Position.Y + Rectangle.Height, Rectangle.Width - 10, 10);
        }

        public override void update(GameTime gameTime)
        {
            updateMovable(gameTime);
        }

        public void updateMovable(GameTime gameTime)
        {
            if (platformType == TypePlatform.Movable)
            {
                Position += Velocity;
                elapsedTime += (float)gameTime.ElapsedGameTime.Milliseconds;
                if (movesVertical)
                {
                    moveVertical();
                }
                else
                {
                    moveHorizontal();
                }
            }
        }

        public void moveVertical()
        {
            if (canMove())
            {
                if (movingUp)
                {
                    moveUp();
                    if (hasReachedVerticalEnd())
                    {
                        movingUp = false;
                        Velocity = Vector2.Zero;
                    }
                }
                else if (!movingUp)
                {
                    moveDown();
                    if (hasReachedVerticalEnd())
                    {
                        movingUp = true;
                        Velocity = Vector2.Zero;
                    }
                }
                elapsedTime = 0f;
            }
        }

        public bool hasReachedVerticalEnd()
        {
            if (movingUp)
            {
                return Position.Y < startPosition.Y - 100;
            }
            else
            {
                return Position.Y > startPosition.Y + 100;
            }
        }

        public bool hasReachedHorizontalEnd()
        {
            if (movingLeft)
            {
                return Position.X < startPosition.X - 100;
            }
            else
            {
                return Position.X > startPosition.X + 100;
            }
        }

        public void moveHorizontal()
        {
            if (canMove())
            {
                if (movingLeft)
                {
                    moveLeft();
                    if (hasReachedHorizontalEnd())
                    {
                        movingLeft = false;
                        Velocity = Vector2.Zero;
                        reachedEndPoint = true;
                    }
                    else
                    {
                        reachedEndPoint = false;
                    }
                }
                else if (!movingLeft)
                {
                    moveRight();
                    if (hasReachedHorizontalEnd())
                    {
                        movingLeft = true;
                        Velocity = Vector2.Zero;
                        reachedEndPoint = true;
                    }
                    else
                    {
                        reachedEndPoint = false;
                    }
                }
                elapsedTime = 0f;
            }
        }

        public bool canMove()
        {
            return elapsedTime >= delay;
        }

        public void moveUp()
        {
            Velocity = new Vector2(Velocity.X, Velocity.Y - 1);
            Velocity.Normalize();
        }

        public void moveDown()
        {
            Velocity = new Vector2(Velocity.X, Velocity.Y + 1);
            Velocity.Normalize();
        }

        public void moveLeft()
        {
            Velocity = new Vector2(Velocity.X - 1, Velocity.Y);
            Velocity.Normalize();
        }

        public void moveRight()
        {
            Velocity = new Vector2(Velocity.X + 1, Velocity.Y);
            Velocity.Normalize();
        }

        public override void loadContent(ContentManager content)
        {
            Texture = content.Load<Texture2D>(TexturePath);
            font = content.Load<SpriteFont>("SpriteFont");
            startPosition = Position;
            initializeRectangles();
        }

        #region Collision

        #region Collision Passable, Impassable, Movable
        public void collisionImpassable(Character character)
        {
            collisionTop(character);
            collisionBottom(character);
            collisionLeft(character);
            collisionRight(character);
        }

        public void collisionMovable(Character character)
        {
            collisionTop(character);
            if (!movesVertical)
            {
                if (movingLeft)
                {
                    character.Position = new Vector2(character.Position.X - 1, character.Position.Y);
                }
                else
                {
                    character.Position = new Vector2(character.Position.X + 1, character.Position.Y);
                }
                if (reachedEndPoint && movingLeft)
                {
                    character.Position = new Vector2(character.Position.X + 2, character.Position.Y);
                }
                else if (reachedEndPoint && !movingLeft)
                {
                    character.Position = new Vector2(character.Position.X - 2, character.Position.Y);
                }
            }
            
        }

        public void collisionPassable(Character character)
        {
            collisionTop(character);
        }
        #endregion

        #region Collision Top, Bottom, Left, Right
        public void collisionTop(Character character)
        {
            if (isColliding(character, top))
            {
                character.Position = new Vector2(character.Position.X, Position.Y - character.Rectangle.Height);
                isTouchingTop = true;
            }
            else
            {
                isTouchingTop = false;
            }
        }

        public bool isColliding(Character character, Rectangle eenPlatformRectangle)
        {
            return eenPlatformRectangle.Intersects(character.Rectangle);
        }

        public void collisionLeft(Character character)
        {
            if (isColliding(character, left))
            {
                if (character.Position.X + character.Rectangle.Width > Position.X)
                {
                    character.Position = new Vector2(Position.X - character.Rectangle.Width, character.Position.Y);
                    isTouchingTop = false;
                }
            }
        }

        public void collisionRight(Character character)
        {
            if (isColliding(character, right))
            {
                if (character.Position.X < Rectangle.Width + Position.X)
                {
                    character.Position = new Vector2(Position.X + Rectangle.Width, character.Position.Y);
                    isTouchingTop = false;
                }
            }
        }

        public void collisionBottom(Character character)
        {
            if (isColliding(character, bottom))
            {
                if (character.Position.Y < Position.Y + Rectangle.Height)
                {
                    character.Velocity = new Vector2(character.Velocity.X, -character.Velocity.Y);
                    isTouchingTop = false;
                }
            }
        }
        #endregion

        public override void collisionWith(Sprite sprite)
        {
            if (sprite is Character)
            {
                Character character = (Character)sprite;
                if (platformType == TypePlatform.Impassable)
                {
                    collisionImpassable(character);
                }
                else if (platformType == TypePlatform.Passable)
                {
                    collisionPassable(character);
                }
                else if (platformType == TypePlatform.Movable)
                {
                    collisionMovable(character);
                }
            }
        }
        #endregion
    }
}
